package com.zhangye.test.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestParam;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

public class JPushController {

	protected static final Logger LOG = LoggerFactory.getLogger(JPushController.class);

	public static JPushClient jpushClient = null;
	private static final String appKey = "dcbeddddc8aa142e500fc792";
	private static final String masterSecret = "601642cac7decdaf26b30a47";

	
	/**
	 * 别名推送
	 * 作者： 张叶
	 * 创建时间：2018年11月13日  下午3:48:10
	 */
	public static void aliasPush(@RequestParam String title) {  
	    jpushClient = new JPushClient(masterSecret, appKey, 3);  
	    PushPayload payload = buildPushObject_alias_alert("123",title);
	    try {  
            PushResult result = jpushClient.sendPush(payload);  
        } catch (Exception e) {  
        	
        }   
    }
	
	/**
	 * 广播推送
	 * 作者： 张叶
	 * 创建时间：2018年11月13日  下午3:15:01
	 */
	public static void testSendPush(@RequestParam String title) {  
	    jpushClient = new JPushClient(masterSecret, appKey, 3);  
	    PushPayload payload = buildPushObject_all_alias_alert(title);
	    try {  
            System.out.println(payload.toString());  
            PushResult result = jpushClient.sendPush(payload);  
            System.out.println(result+"................................");  
              
            LOG.info("Got result - " + result);  
              
        } catch (APIConnectionException e) {  
            LOG.error("Connection error. Should retry later. ", e);  
              
        } catch (APIRequestException e) {  
            LOG.error("Error response from JPush server. Should review and fix it. ", e);  
            LOG.info("HTTP Status: " + e.getStatus());  
            LOG.info("Error Code: " + e.getErrorCode());  
            LOG.info("Error Message: " + e.getErrorMessage());  
            LOG.info("Msg ID: " + e.getMsgId());  
        }   
    }

	  //设置接受平台
	  public static PushPayload buildPushObject_all_alias_alert(String title) {  
	      return PushPayload.newBuilder()  
	              .setPlatform(Platform.all())//设置接受的平台  
	              .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到  
	              .setNotification(Notification.alert(title))//通知内容为 ALERT
//	              .setOptions(Options.newBuilder()
//	    					.setApnsProduction(true)
//	    					.build())
	              .build();  
	  } 

	  //别名
	  public static PushPayload buildPushObject_alias_alert(String alias,String title) {  
	        return PushPayload.newBuilder()
		            .setPlatform(Platform.all())//设置接受的平台  
	                .setAudience(Audience.alias(alias))
	                .setNotification(Notification.alert(title))//通知内容为 ALERT
	                .build();
	  } 

}
